package com.hzncc.flowable_diboot.service.processtransaction.impl;

import com.diboot.core.util.S;
import com.diboot.core.vo.Pagination;
import com.diboot.iam.util.IamSecurityUtils;
import com.hzncc.flowable_diboot.dto.processtransaction.ProcessStartDTO;
import com.hzncc.flowable_diboot.enums.BpmProcessInstanceStatusEnum;
import com.hzncc.flowable_diboot.service.bpm.BpmProcessInstanceService;
import com.hzncc.flowable_diboot.util.DateUtils;
import com.hzncc.flowable_diboot.vo.bpm.BpmProcessInstanceRespVO;
import com.hzncc.flowable_diboot.vo.processtransaction.ProcessStartDetailVO;
import com.hzncc.flowable_diboot.vo.processtransaction.ProcessStartListVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.HistoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.history.HistoricProcessInstance;
import org.flowable.engine.history.HistoricProcessInstanceQuery;
import org.flowable.task.api.TaskQuery;
import org.springframework.stereotype.Service;

import com.diboot.core.service.impl.BaseServiceImpl;

import com.hzncc.flowable_diboot.entity.processtransaction.ProcessStart;

import com.hzncc.flowable_diboot.mapper.processtransaction.ProcessStartMapper;
import com.hzncc.flowable_diboot.service.processtransaction.ProcessStartService;

import java.util.ArrayList;
import java.util.List;


/**
 * 我发起的 相关Service实现类
 *
 * @author MyName
 * @version 1.0
 * @date 2024-11-04
 * Copyright © MyCorp
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class ProcessStartServiceImpl extends BaseServiceImpl<ProcessStartMapper, ProcessStart> implements ProcessStartService {

    private final HistoryService historyService;


    @Override
    public List<ProcessStartListVO> getList(ProcessStartDTO queryDto, Pagination pagination) {
        // 查询我发起的流程
        HistoricProcessInstanceQuery historicProcessInstanceQuery = historyService.createHistoricProcessInstanceQuery()
                .startedBy(IamSecurityUtils.getCurrentUserId());
        if (S.isNotEmpty(queryDto.getBusinessTitle())) {

            historicProcessInstanceQuery.processInstanceNameLike("%" + queryDto.getBusinessTitle() + "%");
        }
        if (queryDto.getStartTimeBegin() != null) {
            historicProcessInstanceQuery.startedAfter(DateUtils.of(queryDto.getStartTimeBegin()));
        }
        if (queryDto.getStartTimeEnd() != null) {
            historicProcessInstanceQuery.startedBefore(DateUtils.of(queryDto.getStartTimeEnd()));
        }
        List<HistoricProcessInstance> list = historicProcessInstanceQuery.listPage((pagination.getPageIndex() - 1) * pagination.getPageSize(), pagination.getPageSize());
        long count = historicProcessInstanceQuery.count();
        pagination.setTotalCount(count);
        List<ProcessStartListVO> processStartList = new ArrayList<>();
        for (HistoricProcessInstance processInstance : list) {
            ProcessStartListVO processStart = convert(processInstance);
            processStartList.add(processStart);
        }
        return processStartList;
    }

    @Override
    public ProcessStartDetailVO getDetailVO(String id) {
        List<HistoricProcessInstance> list = historyService.createHistoricProcessInstanceQuery().processInstanceId(id).list();
        if (list.isEmpty()) {
            return null;
        }
        return convert(list.get(0));
    }

    private ProcessStartDetailVO convert(HistoricProcessInstance processInstance) {
        ProcessStartDetailVO processStart = new ProcessStartDetailVO();
        processStart.setBusinessKey(processInstance.getBusinessKey());
        processStart.setProcessName(processInstance.getProcessDefinitionName());
        processStart.setBusinessTitle(processInstance.getName());
        processStart.setStartTime(DateUtils.of(processInstance.getStartTime()));
        processStart.setEndTime(DateUtils.of(processInstance.getEndTime()));
        processStart.setElapsedTime(processInstance.getDurationInMillis());
        processStart.setProcessProgress(processInstance.getBusinessStatus());
        processStart.setId(processInstance.getId());
        processStart.setProcessDefinitionId(processInstance.getProcessDefinitionId());
        processStart.setProcessDefinitionKey(processInstance.getProcessDefinitionKey());
        processStart.setStartActivityId(processInstance.getStartActivityId());
        processStart.setStartUserId(processInstance.getStartUserId());
        return processStart;
    }
}